$(document).ready(function() {
	if( localStorage['tts_mode'] == 'on')
		$('#activ_tts').attr('src','img/icons/on.png');
	else
		$('#activ_tts').attr('src','img/icons/off.png');
	if( localStorage['helpread_mode'] == 'on')
		$('#activ_helpread').attr('src','img/icons/on.png');
	else
		$('#activ_helpread').attr('src','img/icons/off.png');




    var voiceArray = [];
    chrome.tts.getVoices(function(va) {
            voiceArray = va;
            for (var i = 0; i < voiceArray.length; i++) {
                    if(voiceArray[i].lang ==  null){
						if(voiceArray[i].voiceName == localStorage['voice']){
                            $('.flag_div').append('<div class="flag"><img class="flag_selected_img" src="img/flags/native_flag.png" id="native" onclick="javascript:chooseFlag(this);"/></div>');
						}
						else{
							$('.flag_div').append('<div class="flag"><img src="img/flags/native_flag.png" id="native" onclick="javascript:chooseFlag(this);"/></div>');
						}
					}		
                    else{	
						if(voiceArray[i].voiceName == localStorage['voice']){
                            $('.flag_div').append('<div class="flag"><img class="flag_selected_img" src="img/flags/' + voiceArray[i].lang + '_flag.png" id="'+ voiceArray[i].voiceName +'" onclick="javascript:chooseFlag(this);"/></div>');
						}
						else{
							$('.flag_div').append('<div class="flag"><img src="img/flags/' + voiceArray[i].lang + '_flag.png" id="'+ voiceArray[i].voiceName +'" onclick="javascript:chooseFlag(this);"/></div>');
						}
				 }
            }
            $('.flag_div').append('<div class="clear"></div>');
    });
	
	
});

function updateImageButton(inputImage){
    if(inputImage.src.lastIndexOf('img/icons/on.png',inputImage.src.length) == -1){
        inputImage.src='img/icons/on.png';
        if(inputImage.id == 'activ_tts'){
            localStorage['tts_mode'] = 'on';
        }
        else if(inputImage.id == 'activ_helpread'){
            localStorage['helpread_mode'] = 'on';
        }
    } else {
        inputImage.src='img/icons/off.png';
        if(inputImage.id == 'activ_tts'){
            localStorage['tts_mode'] = 'off';
        }
        else if(inputImage.id == 'activ_helpread'){
            localStorage['helpread_mode'] = 'off';
        }
    }
}

function chooseFlag(object){
	localStorage['voice'] = object.id;
	location.reload();
}


